package org.nowcoder.leetcode.BTree;

/**
 * Title  : 222. Count Complete Tree Nodes
 * Source : https://leetcode.cn/problems/count-complete-tree-nodes/description/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-09-14
 */

public class LeetCode_222 {
    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    private int count = 0;

    private void dfs(TreeNode root) {
        if (root != null) {
            count++;
            dfs(root.left);
            dfs(root.right);
        }
    }

    public int countNodes(TreeNode root) {
        dfs(root);
        return count;
    }
}
